package cn.lxg.controller.admin;

import cn.lxg.pojo.TAdmin;
import cn.lxg.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller(value = "adminAdminController")
@RequestMapping(value = "/page/admin")
public class AdminController {
    @Autowired
    private final IAdminService adminService = null;

    @GetMapping(value = "/edit_admin")
    public String editPage(HttpSession session, Model model) {
        // 准备数据
        TAdmin sessionAdmin = (TAdmin) session.getAttribute("admin");
        TAdmin admin = this.adminService.get(sessionAdmin.getId());

        // 传递数据
        model.addAttribute("admin", admin);

        return "admin/admin/edit";
    }

    @PutMapping(value = "/admin")
    public String edit(HttpSession session, Model model, TAdmin admin) {
        // 准备数据
        TAdmin sessionAdmin = (TAdmin) session.getAttribute("admin");

        // 准备数据
        admin.setId(sessionAdmin.getId());
        String msg = this.adminService.edit(admin);

        // 传递数据
        model.addAttribute("msg", msg);

        return "common/message";
    }
}
